{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "# 用户计算距离\n",
    "import scipy.spatial.distance as ssd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用户特征向量\n",
    "r1 = np.array([1,4,2,1])\n",
    "r2 = np.array([2,4,2,1])\n",
    "r3 = np.array([5,1,5,4])\n",
    "r4 = np.array([2,5,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pearson相关系数\n",
    "def sim(v1, v2):\n",
    "    # 用户1和用户2的平均分打分\n",
    "    v1_mu = sum(v1) / len(v1)\n",
    "    v2_mu = sum(v2) / len(v2)\n",
    "    # 特征向量去均值\n",
    "    v1_ = v1 - v1_mu\n",
    "    v2_ = v2 - v2_mu\n",
    "    # 返回相关系数\n",
    "    return 1 - ssd.cosine(v1_, v2_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户1与用户2的相似度： 0.936585811581694\n",
      "用户1与用户3的相似度： -0.8716019289105665\n",
      "用户1与用户4的相似度： 0.7302967433402214\n"
     ]
    }
   ],
   "source": [
    "# 计算用户1和2、3、4用户的相关系数\n",
    "sim1_2 = sim(r1, r2)\n",
    "sim1_3 = sim(r1, r3)\n",
    "sim1_4 = sim(r1, r4)\n",
    "print('用户1与用户2的相似度：', sim1_2)\n",
    "print('用户1与用户3的相似度：', sim1_3)\n",
    "print('用户1与用户4的相似度：', sim1_4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算用户打分均值\n",
    "r1_mu = sum(r1) / len(r1)\n",
    "r2_mu = sum(r2) / len(r2)\n",
    "r3_mu = sum(r3) / len(r3)\n",
    "r4_mu = sum(r4) / len(r4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算后的预测分数是： 4.047032197970447\n"
     ]
    }
   ],
   "source": [
    "# 带入公式，预测打分\n",
    "predict_rating = r1_mu + ((5 - r2_mu) * sim1_2 + (2 - r3_mu) * sim1_3 + (5 - r4_mu) * sim1_4) / (np.abs(sim1_2) + np.abs(sim1_3) + np.abs(sim1_4))\n",
    "print('计算后的预测分数是：', predict_rating)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最终预测打分： 4.047032197970447\n"
     ]
    }
   ],
   "source": [
    "# 越界问题，评分最低0分，最高5分\n",
    "# predict_rating > 5 --> predict_rating = 5\n",
    "# predict_rating < 0 --> predict_rating = 0\n",
    "predict_rating = np.clip(predict_rating, 0, 5)\n",
    "print('最终预测打分：', predict_rating)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
